package cn.tedu.erp.project.controller;

import cn.tedu.erp.project.pojo.dto.CustomSalesAddNewDTO;
import cn.tedu.erp.project.pojo.dto.CustomSalesUpdateDTO;
import cn.tedu.erp.project.pojo.vo.CustomSalesListVO;
import cn.tedu.erp.project.pojo.vo.CustomSalesStandardVO;
import cn.tedu.erp.project.service.ICustomSalesService;
import cn.tedu.erp.project.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Validated
@Api(tags="销售订单管理模块")
@RequestMapping("/customSales")
@RestController
public class CustomSalesController {
    @Autowired
    private ICustomSalesService service;

    @PostMapping("/add-new")
    @ApiOperation("添加销售订单")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid CustomSalesAddNewDTO customSalesAddNewDTO) {
        log.debug("开始处理【添加销售订单】的请求业务，参数：{}", customSalesAddNewDTO);
        service.addNew(customSalesAddNewDTO);
        return JsonResult.ok();
    }


    @PostMapping("/{id}/delete")
    @ApiOperation("根据id删除销售订单")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value ="销售订单id",required = true,dataType = "Long"),

    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1,message ="请提交有效的id" ) Long id) {
        service.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改销售订单")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value ="销售订单id",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "username",value ="用户名" ,required = true,dataType = "String")
    })
    public JsonResult<Void> update(@PathVariable@Range(min = 1,message ="请提交有效的id" ) Long id,
                                   @Valid CustomSalesUpdateDTO customSalesUpdateDTO) {
        service.updateInfoById(id,customSalesUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation("查询销售订单")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<CustomSalesListVO>> list(){
        log.debug("开始处理【查询列表】的请求，无参数");
        List<CustomSalesListVO> list = service.list();
        return JsonResult.ok(list);

    }


    @PostMapping("/rebuild-cache")
    @ApiOperation("重建销售订单缓存")
    @ApiOperationSupport(order = 900)
    public JsonResult<Void> rebuildCache() {
        log.debug("开始处理【重建销售订单缓存】的请求，无参数");
        service.rebuildCache();
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("查询销售订单详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<CustomSalesStandardVO> getStandardById(@PathVariable @Range(min = 1, message = "请提交有效的ID值！")Long id){
        log.debug("开始处理【查询销售订单详情】的请求，参数:id{}",id);
        CustomSalesStandardVO queryResult = service.getStandardById(id);
        return JsonResult.ok(queryResult);

    }
}
